package com.adopt.controller;

import com.adopt.pojo.Comment;
import com.adopt.pojo.Pet;
import com.adopt.pojo.Result;
import com.adopt.service.ChatHallService;
import com.adopt.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:ChatHallController
 * Package:com.adopt.controller
 * Description:
 *
 * @Date:2023/9/11 14:49
 * Author:shifan
 */
@Controller
@RequestMapping("/chatHall")
public class ChatHallController {
    @Autowired
    private ChatHallService chatHallService;

    @Autowired
    private PetService petService;

    /**
     * 前往交流大厅
     * @return
     */
    @RequestMapping("/toChatHall")
    public String toComment(Integer petId, HttpServletRequest request){
        Pet pet = petService.findById(petId);
        request.getSession().setAttribute("pet",pet);
        return "chat-hall";
    }


    /**
     * 根据动物id获取评论
     * @param petId
     * @return
     */
    @RequestMapping("petComments")
    @ResponseBody
    public Result getPetComment(String petId){
        System.err.println("petId="+petId);
        if (StringUtils.isEmpty(petId)){
            return new Result(100,"petId为空");
        }
        List<Comment> comments = chatHallService.findByPetId(Integer.parseInt(petId));
        if(comments!=null){
            return new Result(200,"获取评论成功",comments);
        }else{
            return new Result(100,"获取评论失败");
        }

    }
}
